﻿package com.element.role
{
	import com.element.movieClip.Animal;
	import com.event.EventZheng;
	import com.IFactory;
	import com.staticInfo.EventConst;
	import com.utils.DebugTrace;
	import flash.events.Event;
	
	import flash.display.Sprite;
	
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	/**
	 * ...
	 * @author zhengzihua
	 */
	public class Man extends Animal
	{
		private var man:*;
		private var intervalID:int = -1;//连续在地图上移动
		private var manSpeed:int = 2;
		private var path:Array;
		private var _manPoint:Point;//起点行，列
		private var walkDirection:String = "down";//角色行走的方向
		private var instance:Man;
		
		private var roleBehavior:IRole;
		public function Man() 
		{
			var MyMan:Class = ApplicationDomain.currentDomain.getDefinition("Man") as Class;
			man = new MyMan();
			man.stop();
			addChild(man);
			instance = this;
			
			roleBehavior = IFactory.getRoleBehavior();
			
		}
		public function walkRoad(arr:Array):void {		
			path = null;
			arr.shift();//因为man，肯定是站在起点上的，因此，第一个点就不用走了。
			path = arr;
			if (intervalID != -1) {
				clearInterval(intervalID);
			}
			addEventListener(EventConst.EVENT_ARRIVETARGET, walkPoint);
			dispatcherInfo()
		}
		
		private function walkPoint(e:EventZheng):void {
			var obj:Object = path.shift();
			if (obj == null) {
				man.gotoAndStop(walkDirection);
				DebugTrace.dtrace("info code Man.as:到达目的点。");
				return;
			}
			//cx,cy为坐标，sx，sy为行列
			var cx:int = obj.cx;
			var cy:int = obj.cy;
			var sx:int = obj.sx;
			var sy:int = obj.sy;
			intervalID = setInterval(function():void {
				if (Math.abs(instance.x -cx) < manSpeed && Math.abs(instance.y -cy) < manSpeed) {
					instance.x = cx;
					instance.y = cy;
					manPoint.x = sx;
					manPoint.y = sy;
					changeRowAndCols(sx, sy);
					obj = null;
					clearInterval(intervalID);
					intervalID = -1;
					dispatcherInfo()
				}else {
					//八方向行走判断
					walkDirection=jugmentDirection(new Point(instance.x, instance.y), new Point(cx, cy))
					walk(walkDirection);
				}
			},50)
		}
		private function dispatcherInfo():void {
			dispatchEvent(new EventZheng(EventConst.EVENT_ARRIVETARGET));//到达
			parent.dispatchEvent(new EventZheng(EventConst.EVENT_ARRIVETARGET));//到达
		}
		private function jugmentDirection(p1:Point, p2:Point):String {//p1为起始点,p2为目标点
			return roleBehavior.jugmentDirection(p1, p2);
		}
		private function walk(str:String):void {
			roleBehavior.walk(str, manSpeed, man,instance);
		}
		private function changeRowAndCols(sx:int,sy:int):void {
			super.sx = sx;
			super.sy = sy;
		}
		public function animal_walk():void {
			
		}
		public function animal_stopWalk():void {
			man.gotoAndStop(walkDirection);
			clearInterval(intervalID);
		}
		
		public function get manPoint():Point { return _manPoint; }
		
		public function set manPoint(value:Point):void 
		{
			_manPoint = value;
		}
		
	}
	
}